//
//  FrontViewController.m
//  SLPagerView
//
//  Created by admin on 2022/1/18.
//

#import "FrontViewController.h"
#import <objc/runtime.h>
#import "Item.h"

@import SLPagerView;
@import MJRefresh;
@import LNZCollectionLayouts;

NSArray *random5(void){
    NSMutableArray *arr = [NSMutableArray new];
    
    for (size_t i = 0; i < 5; i++){
        [arr addObject:[Item new]];
    }
    NSString *clsname = [NSString stringWithFormat:@"UITableViewCell_wadj3_%@", @(arc4random())];
    Class cls = objc_allocateClassPair([UITableViewCell class], clsname.UTF8String, 0);
    return sl_array_map(arr, ^id _Nullable(id  _Nonnull obj) {
        sl_pager_item_add_cell_for_class(obj, ^(id obj){
            return cls;
        });
        return obj;
    });
}

@interface UITableViewCellCC : UITableViewCell

@end

@implementation UITableViewCellCC

-(void)dealloc{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end


@interface FrontViewController ()<SLPagerViewDelete>

@property SLPagerView *pagerview;

@end

@implementation FrontViewController

- (void)dealloc{
    NSLog(@"%s %@", __PRETTY_FUNCTION__, NSStringFromClass(object_getClass(self)));
}
- (UIModalPresentationStyle)modalPresentationStyle{
    return UIModalPresentationFullScreen;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xffffff);

    // Do any additional setup after loading the view.
    SLPagerLayout *layout = [SLPagerLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    SLPagerView *pagerview = [[SLPagerView alloc] initWithFrame:self.view.bounds layout:layout];
    [pagerview observeOnViewController:self];
    pagerview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    pagerview.enableInfinateScroll = NO;
    [self.view addSubview:pagerview];
    self.pagerview = pagerview;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:pagerview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:pagerview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];

    
    NSMutableArray *arr = [NSMutableArray new];
    
    for (size_t i = 0; i < 20; i++){
        [arr addObject:[Item new]];
    }
    pagerview.delegate = self;
    [pagerview setDataSource:sl_array_map(arr, ^id _Nullable(id  _Nonnull obj) {
        sl_pager_item_add_cell_for_class(obj, ^(id obj){
            return [UITableViewCellCC class];
        });
        return obj;
    }) scrollToIndex:0];
    
    pagerview.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [pagerview.collectionView.mj_header endRefreshing];

            [pagerview appendData:random5() atPosition:SLPagerViewDataPositionAfter];
        });
    }];

    pagerview.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [pagerview.collectionView.mj_footer endRefreshing];
            [pagerview appendData:random5() atPosition:SLPagerViewDataPositionAfter];
        });
    }];

    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
    back.frame = CGRectMake(16, 44, 50, 50);
    back.backgroundColor = [UIColor cyanColor];
    back.layer.cornerRadius = 25;
    [back setTitle:@"返回" forState:UIControlStateNormal];
    back.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    back.clipsToBounds = YES;
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];
}
- (void)sl_pagerView:(SLPagerView *)pagerView dequeueView:(id)view atIndexPath:(NSIndexPath*)indexPath withData:(id<SLPagerItem>)item{
    NSLog(@"SLPagerView load %p(%@) %ld", view, NSStringFromClass([view class]),indexPath.row);

    UITableViewCell *cc = view;
    cc.textLabel.text = [NSString stringWithFormat:@"%p(:%ld)", view, indexPath.row];
    if (!cc.backgroundColor){
        cc.backgroundColor = UIColorFromRGB(arc4random());
    }
}
- (void)sl_pagerView:(SLPagerView *)pagerView didFrom:(SLPagerObject *)from to:(SLPagerObject *)to scrolling:(BOOL)isScrolling{
//    NSLog(@"SLPager from %ld(%p) to %ld(%p)", from?from.index:-1, from.view, to?to.index:-1, to.view);
}

@end

